import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-09-04
 * Time: 12:16
 */
public class Test {
    public static int lastStoneWeight(int[] stones) {
        int len = stones.length;
        int right = len - 1;
        for (int i = 0; i < right ; i++) {
            Arrays.sort(stones);
            if(stones[right-1] == 0) break;
            stones[right] -= stones[right-1];
            stones[right-1] = 0;
        }
        return stones[right];
    }
    public static void main(String[] args) {
     int[] arry = {2,7,4,1,8,1,1};
     int ret = lastStoneWeight(arry);
     System.out.println(ret);
    }
    public static boolean CheckPermutation(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        char[] str1 = s1.toCharArray();
        char[] str2 = s2.toCharArray();
        Arrays.sort(str1);
        Arrays.sort(str2);
        return Arrays.equals(str1, str2);
    }

    public static void main13(String[] args) {
        String s1 = "abcva";
        String s2 = "cabvb";
        boolean ret = CheckPermutation(s1,s2);
        System.out.println(ret);
    }
    public static void main12(String[] args) {
        int[] arr = new int[25];
        String s = "asfasdf";
        System.out.println(arr[25]);
    }
    public static int searchInsert(int[] arr,int k){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]  != k){
                count++;
            }else {
                return count;
            }
        }
        if(count == arr.length){
            count = 0;
            for (int i = 0; i < arr.length; i++) {
                if(arr[i] < k){
                    count++;
                }else {
                    return count;
                }
            }
        }
        return count;
    }
    public static void main10(String[] args) {
        int[] arr = {1,2,5,6,87,9,51,52};
        int k = 51;
        int ret = searchInsert(arr,k);
        System.out.println(ret);
    }
    public static void main8(String[] args) {
        Test test = new Test();
        int i = 0;
        test.fermin(i);
        i= i++;
        System.out.println(i);
    }
    void fermin(int i){
        i++;
    }
    public static void main7(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //guessNumber(n);
    }
    public static void main6(String[] args){
        Integer var1=new Integer(1);
        Integer var2=var1;
        doSomething(var2);
        System.out.print(var1.intValue());
        System.out.print(var1==var2);
    }
    public static void doSomething(Integer integer){
        integer=new Integer(2);
    }
    public static void main5(String[] args) {
        int i = 3;
        String result = new String();
        switch (i) {
            case 1:
                result = result + "him ";
            case 2:
                result = result + "her ";
            case 3:
                result = result + "it ";
            default:
                result = result + "me ";
        }
        System.out.println(result);
    }
    static int x;
    public static void main4(String[] args) {
        System.out.println(x);
    }
    static boolean out(char c){
        System.out.print(c);
        return true;
    }
    public static void main3(String[] args) {
        int i = 0;
        for(out('A');out('B') && (i<2);out('C')){
            i++;
            out('D');
        }
    }


    //丑数
    public static boolean isuglily(int n){
        if(n <= 0) return false;
        while (n % 2 == 0){
            n /= 2;
        }
        while (n % 3 == 0){
            n /= 3;
        }
        while (n % 5 == 0){
            n /= 5;
        }
        return n == 1;
    }
    public static boolean isuglily1(int n){
        if(n <= 0) return false;
        int[] arr = {2,3,5};
        for (int x : arr) {
            while (n % x == 0){
                n /= x;
            }
        }
        return n == 1;
    }

    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        boolean ret = isuglily(n);
        System.out.println(ret);
    }
    public static boolean ishappynumber(int n){
        int slow = n;
        int fast = isum(n);
        while (slow != fast){
            slow = isum(slow);
            fast = isum(isum(fast));
        }
        return slow == 1;
    }
    public static int isum(int x){
        int sum = 0;
        while (x > 0){ // 两位以及以上
            int remo = x % 10; // 求尾数
            sum += remo * remo;
            x /= 10;
        }
        return sum;
    }
    public static void main1(String[] args) {
        Scanner scanner  = new Scanner(System.in);
        int n = scanner.nextInt();
        boolean ret = ishappynumber(n);
        System.out.println(ret);
    }
}
